package com.cksys.achievement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cksys.achievement.entity.Role;
import com.cksys.achievement.entity.User;
import com.cksys.achievement.entity.UserRole;
import com.cksys.achievement.enums.EmailStatusEnum;
import com.cksys.achievement.enums.RegisterEnum;
import com.cksys.achievement.exception.MyUsernameNotFoundException;
import com.cksys.achievement.mapper.UserMapper;
import com.cksys.achievement.service.IRoleService;
import com.cksys.achievement.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName: CustomUserDetailsService
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2020/3/8 20:28
 */
@Service
public class CustomUserDetailsServiceImpl implements UserDetailsService {

    public static final String USERNAME_NOT_FOUND = "用户名不存在";
    public static final String NOT_AUDITED = "用户未审核或未通过";
    public static final String NOT_ACTIVATION = "邮箱未激活";

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        // 从数据库中取出用户信息
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));

        // 判断用户是否存在
        if(user == null) {
            throw new MyUsernameNotFoundException(USERNAME_NOT_FOUND);
        }

        if (!StringUtils.isEmpty(user.getEmail())) {
            if (null == user.getEmailFlag() || !user.getEmailFlag().equals(EmailStatusEnum.IS_ACTIVATION)) {
                throw new MyUsernameNotFoundException(NOT_ACTIVATION);
            }
        }

        if (!user.getRegister().equals(RegisterEnum.REGISTER_YES)) {
            throw new MyUsernameNotFoundException(NOT_AUDITED);
        }

        // 添加权限
        List<UserRole> userRoles = userRoleService.list(new QueryWrapper<UserRole>().eq("user_id", user.getId()));
        for (UserRole userRole : userRoles) {
            Role role = roleService.getById(userRole.getRoleId());
            authorities.add(new SimpleGrantedAuthority(role.getName().getRoleName()));
        }

        // 返回UserDetails实现类
        return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), authorities);
    }
}
